<?php
class Account_m extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function error_email($email,$loaibang){ 
        $this->db->select('email');
        $this->db->from($loaibang);    
        $this->db->where('email',$email);
        $this->db->limit(1);
        $query_sm=$this->db->get();
        if($query_sm->num_rows()==1)
        {
            return FALSE;
        }
        else 
        {
            return TRUE;
        }
    }
    public function create_payment($id_mg,$start_time,$end_time,$id_ad,$address,$phone)
    {
         $data = array(   
                        'id_mg'=>$id_mg,
                        'time_start'=>$start_time,
                        'time_end'=>$end_time,     
                        'id_ad'=>$id_ad,
                        'question_money'=>10000,
                        'total_money'=>0,
                        'address'=>$address,
                        'phone'=>$phone
                       );
        $this->db->insert('payment',$data);
    }
    public function add_account($id,$loaibang){ 
        $this->db->select('id_ex');
        $this->db->from($loaibang);    
        $this->db->where('id_ex',$id);
        $this->db->limit(1);
        $query_sm=$this->db->get();
        if($query_sm->num_rows()==1)
        {
            return FALSE;
        }
        else 
        {
            return TRUE;
        }
    
    }
     public function inser_database_ex($id,$password,$a,$email,$adress,$phone,$name,$loaibang){
	$data = array('id_ex'=>$id,
                  'password'=>$password,
                    'id_mg'=>$a,
                    'email'=>$email,
                    'address'=>$adress,
                    'phone'=>$phone,
                    'name'=>$name
                );
	$this->db->insert($loaibang,$data);
     }  
      public function add_account_mk($id_mk,$id_mg,$loaibang){ 
        $this->db->select('id_mk');
        $this->db->from($loaibang);    
        $this->db->where('id_mk',$id_mk);
        $this->db->where('id_mg',$id_mg);
        $this->db->limit(1);
        $query_sm=$this->db->get();
        if($query_sm->num_rows()==1)
        {
            return FALSE;
        }
        else 
        {
            return TRUE;
        }
    
    }
     public function inser_database_mk($id,$password,$email,$adress,$a,$phone,$name,$loaibang){
	$data = array('id_mk'=>$id,
                  'password'=>md5($password),                    
                    'email'=>$email,
                    'address'=>$adress,
                    'id_mg'=>$a,
                    'phone'=>$phone,
                    'name'=>$name
                );
	$this->db->insert($loaibang,$data);
     }  
     
      public function add_account_mg($id,$loaibang){ 
        $this->db->select('id_mg');
        $this->db->from($loaibang);    
        $this->db->where('id_mg',$id);
        $this->db->limit(1);
        $query_sm=$this->db->get();
        if($query_sm->num_rows()==1)
        {
            return FALSE;
        }
        else 
        {
            return TRUE;
        }   
    }
      public function inser_database_mg($id,$password,$email,$adress,$a,$phone,$name,$loaibang,$year){
	$data = array('id_mg'=>$id,
                  'password'=>md5($password),                    
                    'email'=>$email,
                    'address'=>$adress,
                    'id_ad'=>$a,
                    'phone'=>$phone,
                    'name'=>$name,
                    'year'=>$year
                );
	$this->db->insert($loaibang,$data);
     } 
     public function search_database_mg($text,$loaibang)
     {
         /*$this->db->select('name','email');
        $this->db->from($loaibang);    
        $this->db->where('name',$text);
        $this->db->or_where('email',$text);
        $query=$this->db->get();
        return $query->result_array();*/
        $sql="SELECT * FROM $loaibang WHERE id_mg like '".$text."%'";
        $query=$this->db->query($sql);
        return $query->result();
     }
     public function search_database_ex($text,$loaibang)
     {
         /*$this->db->select('name','email');
        $this->db->from($loaibang);    
        $this->db->where('name',$text);
        $this->db->or_where('email',$text);
        $query=$this->db->get();
        return $query->result_array();*/
        $sql="SELECT * FROM $loaibang WHERE id_ex like '".$text."%'";
        $query=$this->db->query($sql);
        return $query->result();
     }
     public function search_database_mk($text,$loaibang)
     {
         /*$this->db->select('name','email');
        $this->db->from($loaibang);    
        $this->db->where('name',$text);
        $this->db->or_where('email',$text);
        $query=$this->db->get();
        return $query->result_array();*/
        $sql="SELECT * FROM $loaibang WHERE id_mk like '".$text."%'";
        $query=$this->db->query($sql);
        return $query->result();
     }
     public function list_users($loaibang)
     {
        $sql="SELECT * FROM $loaibang";
        $query=$this->db->query($sql);
        return $query->result();
     }
     public function list_users1($loaibang,$id_mg)
     {
        $sql="SELECT * FROM $loaibang WHERE id_mg like '$id_mg'";
        $this->db->where('id_mg',$id_mg);
        $query=$this->db->query($sql);
        
        return $query->result();
     }
     public function search_database_null_manager($text,$loaibang)
     {
        $sql="SELECT * FROM $loaibang WHERE id_mg like '".$text."%'";
        $query=$this->db->query($sql);
        if($query->num_rows()==0){
            return FALSE;
        }
        else
            return TRUE;
     }
     public function search_database_null_mk($text,$loaibang)
     {
        $sql="SELECT * FROM $loaibang WHERE id_mk like '".$text."%'";
        $query=$this->db->query($sql);
        if($query->num_rows()==0){
            return FALSE;
        }
        else
            return TRUE;
     }
       public function search_database_null_ex($text,$loaibang)
     {
        $sql="SELECT * FROM $loaibang WHERE id_ex like '".$text."%'";
        $query=$this->db->query($sql);
        if($query->num_rows()==0){
            return FALSE;
        }
        else
            return TRUE;
     }
     public function error_id($id,$loaibang,$id_bang){ 
        $this->db->select($id_bang);
        $this->db->from($loaibang);    
        $this->db->where($id_bang,$id);
        $this->db->limit(1);
        $query_sm=$this->db->get();
        if($query_sm->num_rows()==1)
        {
            return TRUE;
        }
        else 
        {
            return FALSE;
        }
    }

    public function search_admin_pass($id_ad)
    {
        $this->db->select('*');
        $this->db->from('admin');
        $this->db->where('id_ad',$id_ad);        
        $this->db->limit(1);
        $query=$this->db->get();
        return $query->result();
    } 
    public function search_manager_pass($id_mg)
    {
        $this->db->select('*');
        $this->db->from('manager');
        $this->db->where('id_mg',$id_mg);        
        $this->db->limit(1);
        $query=$this->db->get();
        return $query->result();
    } 
    public function search_examiner_pass($id_ex)
    {
        $this->db->select('*');
        $this->db->from('examiner');
        $this->db->where('id_ex',$id_ex);        
        $this->db->limit(1);
        $query=$this->db->get();
        return $query->result();
    }
     public function search_maker_pass($id_mk)
    {
        $this->db->select('*');
        $this->db->from('maker');
        $this->db->where('id_mk',$id_mk);        
        $this->db->limit(1);
        $query=$this->db->get();
        return $query->result();
    } 
     public function update_manager($id_mg,$email_mg,$name_mg,$address_mg,$phone_mg,$year){
         $data = array(                    
                        'email'=>$email_mg,
                        'name'=>$name_mg,
                        'address'=>$address_mg,
                        'phone'=>$phone_mg,
                        'year'=>$year
                );
        $this->db->where('id_mg', $id_mg);
        $this->db->update('manager', $data);
     }
     
     public function update_admin_pass($id_ad,$password){
         $data = array(                    
                        'password'=>md5($password)                       
                );
        $this->db->where('id_ad', $id_ad);
        $this->db->update('admin', $data);
     }
     public function update_manager_pass($id_mg,$password){
         $data = array(                    
                        'password'=>md5($password)                       
                );
        $this->db->where('id_mg', $id_mg);
        $this->db->update('manager', $data);
     }
      public function update_examiner_pass($id_ex,$password){
         $data = array(                    
                        'password'=>$password                       
                );
        $this->db->where('id_ex', $id_ex);
        $this->db->update('examiner', $data);
     }
     public function update_maker_pass($id_mk,$password){
         $data = array(                    
                        'password'=>md5($password)                       
                );
        $this->db->where('id_mk', $id_mk);
        $this->db->update('maker', $data);
     }
     public function get_admin($id_ad)
    {
        $this->db->select('*');
        $this->db->from('admin');
        $this->db->where('id_ad',$id_ad);
        $query=$this->db->get();
        return $query->result();
    }
     public function get_manager($id_mg)
    {
        $this->db->select('*');
        $this->db->from('manager');
        $this->db->where('id_mg',$id_mg);
        $query=$this->db->get();
        return $query->result();
    }
     
     public function get_maker($id_mk)
    {
        $this->db->select('*');
        $this->db->from('maker');
        $this->db->where('id_mk',$id_mk);
        $query=$this->db->get();
        return $query->result();
    }
     public function update_maker($id_mk,$email_mk,$name_mk,$address_mk,$phone_mk,$id_mg){
         $data = array(                    
                        'email'=>$email_mk,
                        'name'=>$name_mk,
                        'address'=>$address_mk,
                        'phone'=>$phone_mk
                );
        $this->db->where('id_mk', $id_mk);
        $this->db->where('id_mg', $id_mg);
        $this->db->update('maker', $data);
     }
    public function delete_manager($id_mg)  
    {
        $this->db->where('id_mg',$id_mg);
        $this->db->delete('manager');
    }
    public function delete_maker($id_mk)  
    {
        $this->db->where('id_mk',$id_mk);
        $this->db->delete('maker');
    }
    
     public function get_examiner($id_ex)
    {
        $this->db->select('*');
        $this->db->from('examiner');
        $this->db->where('id_ex',$id_ex);
        $query=$this->db->get();
        return $query->result();
    }
    
    public function delete_examiner($id_ex)  
    {
        $this->db->where('id_ex',$id_ex);
        $this->db->delete('examiner');
    }
     public function update_examiner($id_ex,$email_ex,$name_ex,$address_ex,$phone_ex){
         $data = array(                    
                        'email'=>$email_ex,
                        'name'=>$name_ex,
                        'address'=>$address_ex,
                        'phone'=>$phone_ex
                );
        $this->db->where('id_ex', $id_ex);
        $this->db->update('examiner', $data);
     }

    
   }
    
?>